<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" class="ele-form-search" label-width="77px" @keyup.enter.native="reload" @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="产品名称:">
              <el-input v-model="where.name" clearable placeholder="请输入产品名称" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button v-if="permission.includes('sys:product:index')" class="ele-btn-icon" icon="el-icon-search" type="primary" @click="reload">
                查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :columns="columns" :datasource="url" :selection.sync="selection" :where="where" height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button v-if="permission.includes('sys:product:add')" class="ele-btn-icon" icon="el-icon-plus" size="small" type="primary" @click="openEdit(null)">
            添加
          </el-button>
        </template>
        <template slot="product_number" slot-scope="{row}">
          <div style="color: #1c6ca1;cursor: pointer;" @click="$router.push('/product/detail/' + row.id)">
            {{ row.product_number }}
          </div>
        </template>
        <template slot="qrcode" slot-scope="{row}">
          <div v-show="row.qrcode ? row.qrcode.status_title === '已关联' : ''" style="color: #1c6ca1;cursor: pointer;" @click="showQrcode(row.qrcode.url, row.qrcode.id)">
            {{ row.qrcode ? row.qrcode.status_title : "" }}
          </div>
          <div v-show="row.qrcode === null" style="color: #1c6ca1;cursor: pointer;" @click="openCodeEdit(row)">
            未关联
          </div>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link v-if="permission.includes('sys:product:edit')" :underline="false" icon="el-icon-edit" type="primary" @click="openEdit(row)">
            修改
          </el-link>
          <el-popconfirm class="ele-action" title="确定要删除此产品吗？" @confirm="remove(row)">
            <el-link v-show="permission.includes('sys:product:delete')" slot="reference" :underline="false" icon="el-icon-delete" type="danger">
              删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>

  </div>
</template>

<script>
import { mapGetters } from "vuex";


export default {
  name: 'SystemLevel',
  components: {

  },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/products',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'qrcode.code',
          label: '二维码编码',
          width: 150,
          align: 'center',
          showOverflowTooltip: true,
          slot: 'product_number',
        },
        {
          prop: 'product_number',
          label: '产品编号',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',
        },
        {
          prop: 'type.name',
          label: '产品类型',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center'
        },
        {
          prop: 'name',
          label: '产品名称',
          align: 'center',
          showOverflowTooltip: true,
          width: 100
        },

        {
          prop: 'customer.name',
          label: '姓名',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'warranty_end',
          label: '电话',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'warranty_status_title',
          label: '数据来源',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center'
        },
        {
          prop: 'warranty_status',
          label: '状态',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center'
        },
        {
          prop: 'user?.name',
          label: '创建人',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'create_time_format',
          label: '创建时间',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'qrcode',
          label: '更新时间',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          slot: 'qrcode',
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 二维码是否显示编辑弹窗
      codeEdit: false,
      // 当前编辑数据
      currentCode: null,
      // 是否显示导入弹窗
      showImport: false
    };
  },
  methods: {
    // 新增产品
    addProduct(value) {
      this.$http.post('products', value).then(res => {
        this.$refs.ProductEdit.loading = false;
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.$refs.ProductEdit.updateVisible(false);
          this.$refs.ProductEdit.customer_name = "";
          this.$refs.ProductEdit.type_name = "";
          this.reload();
        }else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$refs.ProductEdit.loading = false;
        this.$message.error(e.message);
      });
    },
    // 编辑产品
    editProduct(value, id) {
      this.$http.put('products/' + id, value).then(res => {
        this.$refs.ProductEdit.loading = false;
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.$refs.ProductEdit.updateVisible(false);
          this.$refs.ProductEdit.customer_name = "";
          this.$refs.ProductEdit.type_name = "";
          this.reload();
        }
      }).catch(e => {
        this.$refs.ProductEdit.loading = false;
        this.$message.error(e.message);
      });
    },
    // 编辑产品二维码
    openCodeEdit(row) {
      this.$refs.CodeEdit.product_id = row.id;
      this.codeEdit = true;
      this.$refs.CodeEdit.isUpdate = true;

    },
    editQrCode(value, id) {
      const loading = this.$loading({ lock: true });
      this.$http.put('/qr_code/' + id, { product_id: value }).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.$refs.CodeEdit.code_name = "";
          this.$refs.CodeEdit.updateVisible(false);
          loading.close()
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http.delete('/products/' + row.id).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    // 显示二维码
    showQrcode(url, id) {
      this.$refs.showQrcode.dialogVisible = true;
      this.$refs.showQrcode.url = url;
      this.$refs.showQrcode.id = id;
    },
    putQrcode(id) {
      this.$http.put("/qr_code/" + id, { product_id: 0 }).then(res => {
        this.$message({ type: "success", message: res.data.msg })
        this.$refs.showQrcode.dialogVisible = false;
        this.reload();
      })
    }
  }
}
</script>

<style>
.el-autocomplete {
  width: 100%;
}

.el-input {
  width: 100% !important;
}
</style>
